package com.ourteam.product.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractServiceController;
import com.ourteam.frame.web.AbstractSearchHelper;
import com.ourteam.frame.web.GridBean;
import com.ourteam.frame.web.OperationResult;
import com.ourteam.product.ui.dao.ProductUIDomainDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIDomainBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/UI/productUIDomain")
public class ProductUIDomainAPI extends AbstractServiceController {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@RequestMapping("/getProductUIDomainById")
	@ResponseBody
	public ProductUIDomainBean getProductUIDomainById(@RequestParam long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIDomainById(id);
	}

	@RequestMapping("/getProductUIDomains")
	@ResponseBody
	public ProductUIDomainBean[] getProductUIDomains() throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIDomains(null);
	}

	@RequestMapping("/saveProductUIDomain")
	@ResponseBody
	public ProductUIDomainBean saveProductUIDomain(@ModelAttribute ProductUIDomainBean uiDomainBean) throws Exception {
		if (uiDomainBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIDomain(uiDomainBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIDomain(uiDomainBean);
		}
		return uiDomainBean;
	}

	@RequestMapping("/deleteProductUIDomains")
	@ResponseBody
	public OperationResult deleteProductUIDomains(@RequestParam long[] ids) throws Exception {

		PRODUCT_UI_SERVICE.deleteProductUIDomains(ids);

		return OperationResult.SUCCESS_RESULT;
	}

	@RequestMapping("/queryProductUIDomains")
	@ResponseBody
	public GridBean queryProductUIDomains(final @ModelAttribute ProductUIDomainSearchModel searchModel,
			HttpServletRequest request, Model model) throws Exception {

		AbstractSearchHelper<ProductUIDomainDAOQueryBean, ProductUIDomainSearchModel> searchHelper = new AbstractSearchHelper<ProductUIDomainDAOQueryBean, ProductUIDomainSearchModel>(
				searchModel) {

			@Override
			protected ProductUIDomainDAOQueryBean createQueryBean() {
				return new ProductUIDomainDAOQueryBean();
			}

			@Override
			protected int queryCount(ProductUIDomainDAOQueryBean queryBean) throws Exception {
				return PRODUCT_UI_SERVICE.queryProductUIDomainCount(queryBean);
			}

			@Override
			protected Object[] queryData(ProductUIDomainDAOQueryBean queryBean) throws Exception {
				return PRODUCT_UI_SERVICE.queryProductUIDomains(queryBean);
			}

		};

		return searchHelper.doSearch(request);

	}

}
